package com.htudata.node.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htudata.common.enums.NodeTypeEnum;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.node.dao.NodeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class NodeShowService {

  @Autowired
  private NodeDao nodeDao;
  
  public static boolean isValidNodeId(String id) {
    if (!StringOperationUtils.isValidStr2(id))
      return false; 
    if ("-1".equals(id))
      return false; 
    if ("0".equals(id))
      return false; 
    return true;
  }
  
  public static boolean isValidFlowIdId(String id) {
    if (!StringOperationUtils.isValidStr2(id))
      return false; 
    if ("-1".equals(id))
      return false; 
    if ("0".equals(id))
      return false; 
    return true;
  }
  
  public static int getChildNodeTypeId(int nodeTypeId) {
    if (nodeTypeId == NodeTypeEnum.SERVER_GROUP.getCode())
      return NodeTypeEnum.SERVER.getCode(); 
    if (nodeTypeId == NodeTypeEnum.DB_GROUP.getCode())
      return NodeTypeEnum.DB.getCode(); 
    if (nodeTypeId == NodeTypeEnum.MQ_GROUP.getCode())
      return NodeTypeEnum.MQ.getCode(); 
    if (nodeTypeId == NodeTypeEnum.VIRTUAL_STATION.getCode())
      return NodeTypeEnum.VIRTUAL_STATION.getCode(); 
    return -1;
  }
  
  public Map<Long, Map<String, String>> getUserIdsAndNodeIds(List<Long> userIds) throws Exception {
    try {
      Map<Long, Map<String, String>> result = new HashMap<>();
      JSONArray selectNodeIdsByUserId = this.nodeDao.selectNodeIdsByUserId(userIds);
      JSONObject userIdAndNodeId = null;
      for (Object obj : selectNodeIdsByUserId) {
        userIdAndNodeId = (JSONObject)obj;
        if (result.containsKey(Long.valueOf(userIdAndNodeId.getLongValue("userId")))) {
          ((Map)result.get(Long.valueOf(userIdAndNodeId.getLongValue("userId")))).put(userIdAndNodeId.getString("nodeId"), null);
          continue;
        } 
        Map<String, String> map3 = new HashMap<>();
        map3.put(userIdAndNodeId.getString("nodeId"), null);
        result.put(Long.valueOf(userIdAndNodeId.getLongValue("userId")), map3);
      } 
      return result;
    } catch (Exception e) {
       throw e;
    } 
  }
  
  public List<String> getHostIdListByNodeId(String flowId) {
    return this.nodeDao.selectHostIdsByFlowId(flowId);
  }
}
